// pages/edit/edit.js
const {detail, option, update, add, list, follow, faceMatch, session, rubbish, levelOption, baiduIdentify} = require("../../api/index.js")
const des = require('../../utils/des.js')
const utils = require("../../utils/index.js")
Page({

/**
* 页面的初始数据
*/data: {
imgIcon: "../../static/upload.png",
editStatus: false,
baseURL:'',
sessionReadArr:[],
detailList: null,
id: "",
cross:"",
ruleForm:{},
userid:getApp().globalData.userInfo.id,
userInfo:getApp().globalData.userInfo,
ro:{
},
chengguomingcheng:"",
chengguofenleiIndex: 0,
chengguofenleiList: [],
tupian:"",
jianjie:"",
pingdingdengjiList:"优秀,良好,待提升".split(','),
pingdingdengjiIndex:0,
suoshuxingye:"",
yingyongchangjing:"",
jishushixian:"",
chengguoxiangqing:"",
huodeshijian:"请选择时间",
showhuodeshijian:false,
sfsh: '待审核',
shhf:"",
storeupNumber: '0',
clickNumber: '0',
discussNumber:0,
xuehao:"",
xueshengxingming:"",
fuzerenzhanghao:"",
fuzerenxingming:"",
daoshigonghao:"",
chengshi:"",
xianqu:"",
jiedao:"",
xuexiaomingcheng:"",
xueyuanmingcheng:"",
userid:"",
jianjie:"",
yingyongchangjing:"",
jishushixian:"",
},
/**
* 生命周期函数--监听页面加载
*/
async onLoad(options) {
let userid
if (options?.id) {
this.setData({
editStatus: true
})
}
let nowTable = wx.getStorageSync("nowTable");
const res = await session(nowTable)
if(res.data.code==0){
getApp().globalData.userInfo=res?.data
userid = res?.data.id
this.setData({
userid
})
}
let baseURL =wx.getStorageSync('baseURL') + '/'
const id = getApp().globalData.detailId
this.setData({
refid:id,
baseURL
})
//人脸识别
const chengguofenleires = await option('chengguofenlei/chengguofenlei')
chengguofenleires.data.unshift('请选择成果分类')
this.setData({
chengguofenleiList: chengguofenleires.data
})
 let pingdingdengjiList="优秀,良好,待提升".split(',');
 let pingdingdengjiIndex=0;
 this.setData({
   pingdingdengjiList,
   pingdingdengjiIndex
 })
//下 == "下" ${column.refTable} && ${column.refColumn}
 this.setData({
   sfsh: '待审核',
 })
 this.setData({
   storeupNumber: '0',
 })
 this.setData({
   clickNumber: '0',
 })
let  ro=this.data.ro;
if(options?.cross){
var obj = wx.getStorageSync('crossObj');
let refobjempty={}
for (var o in obj){
if(o=='chengguomingcheng'){
   refobjempty["chengguomingcheng"]=obj[o]
ro.chengguomingcheng = true;
continue;
}else{
}
if(o=='chengguofenlei'){
   refobjempty["chengguofenlei"]=obj[o]
ro.chengguofenlei = true;
continue;
}else{
}
if(o=='tupian'){
refobjempty['tupian']=obj[o];
refobjempty['tupianPath']=obj[o].split(",")[0];
ro.tupian = true;
continue;
}else{
}
if(o=='jianjie'){
   refobjempty["jianjie"]=obj[o]
ro.jianjie = true;
continue;
}else{
}
if(o=='pingdingdengji'){
   refobjempty["pingdingdengji"]=obj[o]
ro.pingdingdengji = true;
continue;
}else{
}
if(o=='suoshuxingye'){
   refobjempty["suoshuxingye"]=obj[o]
ro.suoshuxingye = true;
continue;
}else{
}
if(o=='yingyongchangjing'){
   refobjempty["yingyongchangjing"]=obj[o]
ro.yingyongchangjing = true;
continue;
}else{
}
if(o=='jishushixian'){
   refobjempty["jishushixian"]=obj[o]
ro.jishushixian = true;
continue;
}else{
}
if(o=='chengguoxiangqing'){
getApp().globalData.editorContent=obj[o]
ro.chengguoxiangqing = true;
continue;
}else{
}
if(o=='huodeshijian'){
   refobjempty["huodeshijian"]=obj[o]
ro.huodeshijian = true;
continue;
}else{
}
if(o=='storeupNumber'){
   refobjempty["storeupNumber"]=obj[o]
ro.storeupNumber = true;
continue;
}else{
}
if(o=='clickNumber'){
   refobjempty["clickNumber"]=obj[o]
ro.clickNumber = true;
continue;
}else{
}
if(o=='thumbsupNumber'){
   refobjempty["thumbsupNumber"]=obj[o]
ro.thumbsupNumber = true;
continue;
}else{
}
if(o=='crazilyNumber'){
   refobjempty["crazilyNumber"]=obj[o]
ro.crazilyNumber = true;
continue;
}else{
}
if(o=='discussNumber'){
   refobjempty["discussNumber"]=obj[o]
ro.discussNumber = true;
continue;
}else{
}
if(o=='xuehao'){
   refobjempty["xuehao"]=obj[o]
ro.xuehao = true;
continue;
}else{
}
if(o=='xueshengxingming'){
   refobjempty["xueshengxingming"]=obj[o]
ro.xueshengxingming = true;
continue;
}else{
}
if(o=='fuzerenzhanghao'){
   refobjempty["fuzerenzhanghao"]=obj[o]
ro.fuzerenzhanghao = true;
continue;
}else{
}
if(o=='fuzerenxingming'){
   refobjempty["fuzerenxingming"]=obj[o]
ro.fuzerenxingming = true;
continue;
}else{
}
if(o=='daoshigonghao'){
   refobjempty["daoshigonghao"]=obj[o]
ro.daoshigonghao = true;
continue;
}else{
}
if(o=='chengshi'){
   refobjempty["chengshi"]=obj[o]
ro.chengshi = true;
continue;
}else{
}
if(o=='xianqu'){
   refobjempty["xianqu"]=obj[o]
ro.xianqu = true;
continue;
}else{
}
if(o=='jiedao'){
   refobjempty["jiedao"]=obj[o]
ro.jiedao = true;
continue;
}else{
}
if(o=='xuexiaomingcheng'){
   refobjempty["xuexiaomingcheng"]=obj[o]
ro.xuexiaomingcheng = true;
continue;
}else{
}
if(o=='xueyuanmingcheng'){
   refobjempty["xueyuanmingcheng"]=obj[o]
ro.xueyuanmingcheng = true;
continue;
}else{
}
if(o=='userid'){
   refobjempty["userid"]=obj[o]
ro.userid = true;
continue;
}else{
}
}
let  statusColumnName=wx.getStorageSync('statusColumnName');
statusColumnName=statusColumnName.replace('[',"").replace(']',"");
this.setData({
ro,
cross:options?.cross,
statusColumnName
})
this.setData(refobjempty)
}
if(id){
// 如果上一级页面传递了id，获取改id数据信息
const data=getApp().globalData.detailList;
let dataEmpty={};
chengguofenleires.data.map((item, index) => {
if (item == data.chengguofenlei) {
 this.setData({
chengguofenleiIndex: index,
chengguofenlei: item
 })
}else if (this.data.chengguofenleiList.length == 1) {
this.setData({
chengguofenleiIndex: 0,
chengguofenlei: v
 })
}
})
const  def_5= 0  ;
this.data.pingdingdengjiList.map((v, index) => {
if (v == data.pingdingdengji|| (v == def_5 && def_5 !=null)) {
 this.setData({
   pingdingdengjiIndex: index,
   pingdingdengji: v,
 })
}else if (this.data.pingdingdengjiList.length == 1) {
 this.setData({
   pingdingdengjiIndex: 0,
   pingdingdengji: v,
 })
}
})
for (var o in data){
if(o=='chengguomingcheng') {
 if(!this.data.chengguomingcheng){
  dataEmpty["chengguomingcheng"] = data[o]
 }
}
if(o=='chengguofenlei') {
 if(!this.data.chengguofenlei){
  dataEmpty["chengguofenlei"] = data[o]
 }
}
if(o=='tupian') {
dataEmpty['tupian'] = data[o];
dataEmpty['tupianPath'] = data[o].split(",")[0];
}
if(o=='jianjie') {
 if(!this.data.jianjie){
  dataEmpty["jianjie"] = data[o]
 }
}
if(o=='pingdingdengji') {
 if(!this.data.pingdingdengji){
  dataEmpty["pingdingdengji"] = data[o]
 }
}
if(o=='suoshuxingye') {
 if(!this.data.suoshuxingye){
  dataEmpty["suoshuxingye"] = data[o]
 }
}
if(o=='yingyongchangjing') {
 if(!this.data.yingyongchangjing){
  dataEmpty["yingyongchangjing"] = data[o]
 }
}
if(o=='jishushixian') {
 if(!this.data.jishushixian){
  dataEmpty["jishushixian"] = data[o]
 }
}
if(o=='chengguoxiangqing') {
getApp().globalData.editorContent = data[o];
}
if(o=='huodeshijian') {
 if(!this.data.huodeshijian){
  dataEmpty["huodeshijian"] = data[o]
 }
}
if(o=='storeupNumber') {
 if(!this.data.storeupNumber){
  dataEmpty["storeupNumber"] = data[o]
 }
}
if(o=='clickNumber') {
 if(!this.data.clickNumber){
  dataEmpty["clickNumber"] = data[o]
 }
}
if(o=='thumbsupNumber') {
 if(!this.data.thumbsupNumber){
  dataEmpty["thumbsupNumber"] = data[o]
 }
}
if(o=='crazilyNumber') {
 if(!this.data.crazilyNumber){
  dataEmpty["crazilyNumber"] = data[o]
 }
}
if(o=='discussNumber') {
 if(!this.data.discussNumber){
  dataEmpty["discussNumber"] = data[o]
 }
}
if(o=='xuehao') {
 if(!this.data.xuehao){
  dataEmpty["xuehao"] = data[o]
 }
}
if(o=='xueshengxingming') {
 if(!this.data.xueshengxingming){
  dataEmpty["xueshengxingming"] = data[o]
 }
}
if(o=='fuzerenzhanghao') {
 if(!this.data.fuzerenzhanghao){
  dataEmpty["fuzerenzhanghao"] = data[o]
 }
}
if(o=='fuzerenxingming') {
 if(!this.data.fuzerenxingming){
  dataEmpty["fuzerenxingming"] = data[o]
 }
}
if(o=='daoshigonghao') {
 if(!this.data.daoshigonghao){
  dataEmpty["daoshigonghao"] = data[o]
 }
}
if(o=='chengshi') {
 if(!this.data.chengshi){
  dataEmpty["chengshi"] = data[o]
 }
}
if(o=='xianqu') {
 if(!this.data.xianqu){
  dataEmpty["xianqu"] = data[o]
 }
}
if(o=='jiedao') {
 if(!this.data.jiedao){
  dataEmpty["jiedao"] = data[o]
 }
}
if(o=='xuexiaomingcheng') {
 if(!this.data.xuexiaomingcheng){
  dataEmpty["xuexiaomingcheng"] = data[o]
 }
}
if(o=='xueyuanmingcheng') {
 if(!this.data.xueyuanmingcheng){
  dataEmpty["xueyuanmingcheng"] = data[o]
 }
}
if(o=='userid') {
 if(!this.data.userid){
  dataEmpty["userid"] = data[o]
 }
}
}
const url = wx.getStorageSync("baseURL") + "/"
const detailList = data
let  objtemp= {
detailList,
 tupian:data?.tupian?.split(',')[0],
 tupianPath:data?.tupian?.split(',')[0],
 huodeshijian:utils.getCurrentDate("yMDhms"),
 sfsh: '待审核',
 storeupNumber: '0',
 clickNumber: '0',
}
this.data.editStatus? getApp().globalData.editorContent=data.chengguoxiangqing:""
this.setData(objtemp);

this.setData(dataEmpty)
}else {
this.setData({
})
}
// ss读取
let sessionReadArr=[]
this.setData({
cross:options?.cross,
ro,
id,
})

if (wx.getStorageSync('raffleType') ) {
 wx.removeStorageSync('raffleType')
 setTimeout(() => {
  this.submit()
 }, 300)
}










this.setData({
huodeshijian:utils.getCurrentDate("yMDhms")
})



















},
getUUID () {
return new Date().getTime();
},
onUnload: function () {
getApp().globalData.editorContent = ''
getApp().globalData.editorContent = ''
getApp().globalData.editorContent = ''
getApp().globalData.editorContent = ''
},
onShow() {
},
 commentScore(e){
 if(this.data?.commentScore){
  this.setData({
   commentScore:e.detail
  })
 }
 },






//chengguofenlei, 0
// 下拉变化
chengguofenleiChange(e) {
this.setData({
 chengguofenleiIndex:   e.detail.value,
 chengguofenlei :this.data.chengguofenleiList[e.detail.value]
})
},

//pingdingdengji, 0
// 下拉变化
pingdingdengjiChange(e) {
this.setData({
 pingdingdengjiIndex:   e.detail.value,
 pingdingdengji :this.data.pingdingdengjiList[e.detail.value]
})
},










uploadtupian() {
wx.chooseImage({
count: 1,
sizeType: ['compressed'],
sourceType: ['album', 'camera'],
success: async (res) => {
const tempFilePaths = res.tempFilePaths;
// 本地临时图片的路径
this.setData({
 tupianPath: tempFilePaths[0]
})
let _this = this;
// 上传网络图片
const baseURL = wx.getStorageSync('baseURL')
wx.uploadFile({
url: `${baseURL}/file/upload`,
filePath: res.tempFilePaths[0],
name: 'file',
header: {
 'Token': wx.getStorageSync('token')
},
success: (uploadFileRes) => {
 let result = JSON.parse(uploadFileRes.data);
// result.file是上传成功为网络图片的名称
 if (result.code == 0) {
  this.setData({
    tupian: 'file/' + result.file
  })
 } else {
  wx.showToast({
   title: result.msg,
   icon: 'none',
   duration: 2000
  });
 }
}
})

this.setData({
face: tempFilePaths[0]
});

}
})
},




jianjieInput(e){
this.setData({
jianjie: e.detail.value // 每次输入变化时更新文本框的值
});},




jianjieInput(e) {
this.setData({
jianjie: e.detail.value // 每次输入变化时更新文本框的值
});
},








yingyongchangjingInput(e){
this.setData({
yingyongchangjing: e.detail.value // 每次输入变化时更新文本框的值
});},




yingyongchangjingInput(e) {
this.setData({
yingyongchangjing: e.detail.value // 每次输入变化时更新文本框的值
});
},
jishushixianInput(e){
this.setData({
jishushixian: e.detail.value // 每次输入变化时更新文本框的值
});},




jishushixianInput(e) {
this.setData({
jishushixian: e.detail.value // 每次输入变化时更新文本框的值
});
},











 onhuodeshijianTap(){
this.setData({
showhuodeshijian: true,
})
let c = this.data;
},
huodeshijianTap(e) {
this.setData({
huodeshijian: e.detail.data
})

let c = this.data;
},




shhfInput(e){
this.setData({
shhf: e.detail.value // 每次输入变化时更新文本框的值
});},




shhfInput(e) {
this.setData({
shhf: e.detail.value // 每次输入变化时更新文本框的值
});
},


































































async submit() {
let that = this
var query = wx.createSelectorQuery();

// 在 Page 中的某个方法中调用
query.select('#jianjie').boundingClientRect();
query.exec(function (res) {
//res就是 所有标签为v1的元素的信息 的数组
that.setData({
jianjie: res[0]?.dataset?.info
})
})
// 在 Page 中的某个方法中调用
query.select('#yingyongchangjing').boundingClientRect();
query.exec(function (res) {
//res就是 所有标签为v1的元素的信息 的数组
that.setData({
yingyongchangjing: res[0]?.dataset?.info
})
})
// 在 Page 中的某个方法中调用
query.select('#jishushixian').boundingClientRect();
query.exec(function (res) {
//res就是 所有标签为v1的元素的信息 的数组
that.setData({
jishushixian: res[0]?.dataset?.info
})
})
if(this.data.huodeshijian?.includes("请选择") || this.data.huodeshijian==""){
wx.showToast({
icon: "none",
title: `获得时间不能为空`,
})
return
}

const baseURL = wx.getStorageSync('baseURL') + "/"
const regex = new RegExp(baseURL, "g");
const obj={
chengguomingcheng: this.data.chengguomingcheng,
chengguofenlei:this.data.chengguofenleiList[this.data.chengguofenleiIndex]?this.data.chengguofenleiList[this.data.chengguofenleiIndex]:'',
tupian:this.data.tupian?.replace(regex, ""),
jianjie: this.data.jianjie,
pingdingdengji:this.data.pingdingdengjiList[this.data.pingdingdengjiIndex]?this.data.pingdingdengjiList[this.data.pingdingdengjiIndex]:'',
suoshuxingye: this.data.suoshuxingye,
yingyongchangjing: this.data.yingyongchangjing,
jishushixian: this.data.jishushixian,
chengguoxiangqing: getApp().globalData.editorContent,
huodeshijian: this.data.huodeshijian,
shhf:this.data.shhf,
storeupNumber: this.data.storeupNumber,
clickNumber: this.data.clickNumber?this.data.clickNumber:"0",
discussNumber: this.data.discussNumber,
xuehao: this.data.xuehao,
xueshengxingming: this.data.xueshengxingming,
fuzerenzhanghao: this.data.fuzerenzhanghao,
fuzerenxingming: this.data.fuzerenxingming,
daoshigonghao: this.data.daoshigonghao,
chengshi: this.data.chengshi,
xianqu: this.data.xianqu,
jiedao: this.data.jiedao,
xuexiaomingcheng: this.data.xuexiaomingcheng,
xueyuanmingcheng: this.data.xueyuanmingcheng,
userid: this.data.userid,
 jianjie:getApp().globalData.editorContent,
 yingyongchangjing:getApp().globalData.editorContent,
 jishushixian:getApp().globalData.editorContent,
}
const detailId= getApp().globalData.detailId
const tableName= `xiangmuchengguo`
//跨表计算判断
var obj2;
var  ruleForm=obj
obj2 = ruleForm
this.data.refid==""? ruleForm['refid']= getApp().globalData.detailId:""
ruleForm['userid']=getApp().globalData.userInfo.id;
var userInfo=getApp().globalData.userInfo
const sessionReadArr=this.data.sessionReadArr
const phonePattern = /^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/;
const mobilePattern = /^(?:\+?86)?1[3-9]\d{9}$/;
const emailPattern = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/;
const idPattern = /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[1-2]\d|3[0-1])\d{3}[\dxX]$/;
const urlPattern = /^(http|https):\/\/[\w\-]+(\.[\w\-]+)+[/#?]?.*$/;
 if(!this.data.chengguomingcheng ){
  wx.showToast({
   icon: "none",
   title: `成果名称不能为空`,
  })
  return
 }





 if(!this.data.chengguofenlei ){
  wx.showToast({
   icon: "none",
   title: `成果分类不能为空`,
  })
  return
 }













 if(!this.data.pingdingdengji ){
  wx.showToast({
   icon: "none",
   title: `评定等级不能为空`,
  })
  return
 }





















let chengguoxiangqing=  getApp().globalData.editorContent
ruleForm['chengguoxiangqing']=getApp().globalData.editorContent
if(chengguoxiangqing==='' ){
wx.showToast({
icon: "none",
title: `成果详情不能为空`,
})
return
}













const valuestoreupNumber = this.data.storeupNumber;
const isIntegerstoreupNumber = /^[-+]?\d+$/.test(valuestoreupNumber);
if(!isIntegerstoreupNumber && this.data.storeupNumber){
wx.showToast({
icon: "none",
title: `收藏数量应输入整数`,
})
return
}





const valueclickNumber = this.data.clickNumber;
const isIntegerclickNumber = /^[-+]?\d+$/.test(valueclickNumber);
if(!isIntegerclickNumber && this.data.clickNumber){
wx.showToast({
icon: "none",
title: `点击次数应输入整数`,
})
return
}





const valuethumbsupNumber = this.data.thumbsupNumber;
const isIntegerthumbsupNumber = /^[-+]?\d+$/.test(valuethumbsupNumber);
if(!isIntegerthumbsupNumber && this.data.thumbsupNumber){
wx.showToast({
icon: "none",
title: `赞应输入整数`,
})
return
}





const valuecrazilyNumber = this.data.crazilyNumber;
const isIntegercrazilyNumber = /^[-+]?\d+$/.test(valuecrazilyNumber);
if(!isIntegercrazilyNumber && this.data.crazilyNumber){
wx.showToast({
icon: "none",
title: `踩应输入整数`,
})
return
}





const valuediscussNumber = this.data.discussNumber;
const isIntegerdiscussNumber = /^[-+]?\d+$/.test(valuediscussNumber);
if(!isIntegerdiscussNumber && this.data.discussNumber){
wx.showToast({
icon: "none",
title: `评论数应输入整数`,
})
return
}

















































//更新跨表属性
var crossuserid;
var crossrefid;
var crossoptnum;
if(this.data.cross) {
wx.setStorageSync('crossCleanType', true);
var statusColumnName = wx.getStorageSync('statusColumnName');
var statusColumnValue = wx.getStorageSync('statusColumnValue');
if (statusColumnName != '') {
obj2 = wx.getStorageSync('crossObj');
if (!statusColumnName.startsWith("[")) {
for (var o in obj2) {
if (statusColumnName==o){
obj2[o] = statusColumnValue;
}

}
var table = wx.getStorageSync('crossTable');

await update(table, obj2)
} else {

crossuserid =getApp().globalData.userInfo.id
crossrefid =  this.data.id
crossoptnum = wx.getStorageSync('statusColumnName');
crossoptnum = crossoptnum.replace(/\[/, "").replace(/\]/, "");
}
}
}
 ruleForm?.shhf?ruleForm.shhf=this.data.shhf:''
if(crossrefid && crossuserid) {
ruleForm['crossuserid'] =getApp().globalData.userInfo.id;
ruleForm['crossrefid'] =obj2.id;
this.setData({
ruleForm
})
let params = {
page: 1,
limit: 10,
crossuserid: crossuserid,
crossrefid: crossrefid,
}
const tips = wx.getStorageSync('tips')
let corssRes = await list(`xiangmuchengguo`, params)
crossoptnum = wx.getStorageSync('statusColumnName');
crossoptnum = crossoptnum.match(/\d+/g);
if (corssRes?.data?.total >= parseInt(crossoptnum)) {
wx.showToast({
icon: "none",
title: tips,
})
wx.removeStorageSync('crossCleanType');
return ;
}
else {
//跨表计算
if (ruleForm.id ) {
await update(`xiangmuchengguo`, ruleForm)
}
else {
 this.data?.commentScore?ruleForm['score']= this.data.commentScore:'';
 await add(`xiangmuchengguo`, ruleForm)
}
}
}
else {
//跨表计算
if (ruleForm.id || this.data.editStatus) {
this.data.editStatus?ruleForm['id']= getApp().globalData.detailId:""
await update(`xiangmuchengguo`, ruleForm)
}
else {
 this.data?.commentScore?ruleForm['score']= this.data.commentScore:'';
await add(`xiangmuchengguo`, ruleForm)
}
}
getApp().globalData.editorContent=''
wx.showToast({
title: '提交成功',
icon: "none"
})
const preId = getApp().globalData.detailId

if(table){
let res = await detail(table, preId)
if(res.code==0){
getApp().globalData.detailList = res.data
}

}
wx.navigateBack({
delta: 1,
complete: () => {
// 触发事件通知，传递需要更新的数据
const pages = getCurrentPages();
if (pages.length >= 1) {
const prePage = pages[pages.length - 1];
prePage.onLoad(); //
}
}
})

},
onHide() {

},


/**
* 生命周期函数--监听页面卸载
*/
onUnload() {

},


/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {

},

/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {

},

/**
* 用户点击右上角分享
*/
onShareAppMessage() {

}
})